package schoolsys.controller;





import schoolsys.entity.Student;
import schoolsys.service.StudentService;
import schoolsys.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
/**
 * author:wxy
 * create_Date:2021/10/19 11:05
 * 添加学生servlet
 * tb_name:student
 */
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //先处理中文乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //设置请求内容类型编码
        response.setContentType("text/html;utf-8");
        PrintWriter out=response.getWriter();
        StudentService studentService=new StudentServiceImpl();
        String studentNo=request.getParameter("studentNo");
        String studentName=request.getParameter("studentName");
        String phone=request.getParameter("phone");
        String address=request.getParameter("address");
        String email=request.getParameter("email");
        Student stu=new Student();
        stu.setAddress(address);
        stu.setEmail(email);
        stu.setStudentName(studentName);
        stu.setPhone(phone);
        if(studentNo!=null&&!"".equals(studentNo)){
            stu.setStudentNo(Integer.parseInt(studentNo));
        }
        //添加到数据库
         boolean flag=studentService.addStu(stu);
        if(flag){
            out.println("添加成功");
        }else{
            out.println("添加失败");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doGet(request,response);
    }
}
